#include "DrawText.h"
#include <fstream>
#include <sstream>
#include "../SGD_Wrappers/CSGD_TextureManager.h"


CDrawText* CDrawText::GetInstance( void )
{
	static CDrawText s_Instance;

	return &s_Instance;
}

CDrawText::CDrawText(void)
{
}


CDrawText::~CDrawText(void)
{
}

void CDrawText::Print( int fontIndex, const char* szText, int nX, int nY, float fScale, DWORD dwColor )
{
	CSGD_TextureManager* m_pTM = CSGD_TextureManager::GetInstance();
	// Keep track of the starting x value (for newlines)
	int nColStart = nX;

	// Iterate through the string 1 character at a time
	int nLength = strlen( szText );

	for( int i = 0; i < nLength; i++ )
	{
		// Get the character out of the string
		char ch = szText[ i ];

		// Is it a space?
		if( ch == '\t' )
		{
			// Skip over some pixels
			nX += (int)(m_Charset.Chars[ch].Width * 3 * fScale);
			continue;
		}
		else if( ch == '\n' )
		{
			// Move down some pixels
			nY += (int)(m_Charset.LineHeight * fScale);

			// Reset the x
			nX = nColStart;
			continue;
		}

		RECT temp;

		temp.left	= m_Charset.Chars[ch].x;
		temp.right	= m_Charset.Chars[ch].x + m_Charset.Chars[ch].Width;;
		temp.top	= m_Charset.Chars[ch].y;
		temp.bottom = m_Charset.Chars[ch].y + m_Charset.Chars[ch].Height;

		// Draw the character
		m_pTM->Draw(fontIndex, nX, nY + (int)( m_Charset.Chars[ch].YOffset * fScale ), fScale, fScale, &temp, 0, 0, 0, dwColor);

		// Move to the next location on screen
		nX += (int)(m_Charset.Chars[ch].XAdvance * fScale);
	}
}

bool CDrawText::ParseFont( char* name )
{
	std::ifstream Stream(name);
	std::string Line;
	std::string Read, Key, Value;
	std::size_t i;

	while( !Stream.eof() )
	{
		std::stringstream LineStream;
		std::getline( Stream, Line );
		LineStream << Line;

		//read the line's type
		LineStream >> Read;
		if( Read == "common" )
		{
			//this holds common data
			while( !LineStream.eof() )
			{
				std::stringstream Converter;
				LineStream >> Read;
				i = Read.find( '=' );
				Key = Read.substr( 0, i );
				Value = Read.substr( i + 1 );

				//assign the correct value
				Converter << Value;
				if( Key == "lineHeight" )
					Converter >> m_Charset.LineHeight;
				else if( Key == "base" )
					Converter >> m_Charset.Base;
				else if( Key == "scaleW" )
					Converter >> m_Charset.Width;
				else if( Key == "scaleH" )
					Converter >> m_Charset.Height;
				else if( Key == "pages" )
					Converter >> m_Charset.Pages;
			}
		}
		else if( Read == "char" )
		{
			//this is data for a specific char
			unsigned short CharID = 0;

			while( !LineStream.eof() )
			{
				std::stringstream Converter;
				LineStream >> Read;
				i = Read.find( '=' );
				Key = Read.substr( 0, i );
				Value = Read.substr( i + 1 );

				if( CharID > 255){
					break;
				}

				//assign the correct value
				Converter << Value;
				if( Key == "id" )
					Converter >> CharID;
				else if( Key == "x" )
					Converter >> m_Charset.Chars[CharID].x;
				else if( Key == "y" )
					Converter >> m_Charset.Chars[CharID].y;
				else if( Key == "width" )
					Converter >> m_Charset.Chars[CharID].Width;
				else if( Key == "height" )
					Converter >> m_Charset.Chars[CharID].Height;
				else if( Key == "xoffset" )
					Converter >> m_Charset.Chars[CharID].XOffset;
				else if( Key == "yoffset" )
					Converter >> m_Charset.Chars[CharID].YOffset;
				else if( Key == "xadvance" )
					Converter >> m_Charset.Chars[CharID].XAdvance;
				else if( Key == "page" )
					Converter >> m_Charset.Chars[CharID].Page;
				else if( Key == "chnl" ){
					Converter >> m_Charset.Chars[CharID].Chnl;
				}
			}
		}
	}

	Stream.close();
	return true;
}
